package com.sos.core
{
	import com.sos.utils.ClassUtils;

	public class TargetType
	{
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------	
		
		// Enum values
		public static const NONE:TargetType					= new TargetType(0, "NONE",			"none");	// no target 
		public static const POSITION:TargetType				= new TargetType(1, "POSITION",		"target");	// can target only defined position
		public static const EFFECT:TargetType				= new TargetType(2, "EFFECT",		"effect");  // can target only defined effects
		public static const GRAVEYARD:TargetType			= new TargetType(3, "GRAVEYARD",	"card");	// can target only graveyard cards
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<TargetType>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<TargetType>();
			_list.push(TargetType.NONE);
			_list.push(TargetType.POSITION);
			_list.push(TargetType.EFFECT);
			_list.push(TargetType.GRAVEYARD);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		private var _description:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function TargetType(id:uint, name:String, description:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("TargetType enum already created!");
			// Init
			_id = id;
			_name = name;
			_description = description;
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------	
		
		public function get id():uint
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		public function get description():String
		{
			return _description;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * 
		 * @return 
		 */
		public static function names():Array
		{
			var list:Array = new Array();
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * 
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array  = new Array();
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
		
		/**
		 * Search a TargetType by given id 
		 * @param id Id of the card type to find
		 * @return CardTargetType constant for the given id, null if no CardTargetType
		 * for the given id has been found
		 */		
		public static function getById(id:uint):TargetType
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("TargetType for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a TargetType by given name 
		 * @param name Name of the player role to find
		 * @return PlayerRole constant for the given id, null if no PlayerRole
		 * for the given id has been found
		 */		
		public static function getByName(name:String):TargetType
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("TargetType for name '" + name + "' unknown!");
		}
		
		/**
		 * Search a TargetType by given name 
		 * @param name Name of the player role to find
		 * @return PlayerRole constant for the given id, null if no PlayerRole
		 * for the given id has been found
		 */		
		public static function getByDescription(description:String):TargetType
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].description == description) return _list[i];
			}
			throw new Error("TargetType for description '" + description + "' unknown!");
		}
	}
}